package com.lht.leetcodeDemo;

import org.junit.Test;

/**
 * @author Eric
 * @version 1.0
 * @date 2019-12-10
 */
public class MedianofTwoSortedArrays {

    @Test
    public void test() {
        System.out.println(new MedianofTwoSortedArrays().findMedianSortedArrays(new int[]{1, 2}, new int[]{3, 4}));
    }

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        // 本质是排序
        int[] num = new int[nums1.length + nums2.length];
        int i = 0, f = 0, s = 0;
        while (f < nums1.length && s < nums2.length) {
            num[i++] = nums1[f] < nums2[s] ? nums1[f++] : nums2[s++];
        }
        while (f < nums1.length) {
            num[i++] = nums1[f++];
        }
        while (s < nums2.length) {
            num[i++] = nums2[s++];
        }
        int mid = num.length >>> 1;
        if (num.length % 2 == 0) {
            return (num[mid - 1] + num[mid]) * 1.0 / 2;
        } else {
            return num[mid];
        }
    }
}

